calculate number of years months and days between two dates in java

118

java number of days between dates -

LocalDate start = LocalDate.parse("2020-11-03");
LocalDate end = LocalDate.parse("2020-12-15");

long diff = DAYS.between(start, end);

how to calculate the amount of months between two dates java -

LocalDate jamesBirthDay = new LocalDate(1955, 5, 19);
LocalDate now = new LocalDate(2015, 7, 30);
        
int monthsBetween = Months.monthsBetween(jamesBirthDay, now).getMonths();
int yearsBetween = Years.yearsBetween(jamesBirthDay, now).getYears();
        
System.out.println("Month since father of Java born : " 
                    + monthsBetween);
System.out.println("Number of years since father of Java born : " 
                    + yearsBetween);

Comments

Submit
0 Comments